package com.xinyou.u8c.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyou.u8c.entity.*;
import com.xinyou.u8c.result.JsonResult;
import com.xinyou.u8c.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * U8字段 前端控制器
 * </p>
 *
 * @author chq
 * @since 2023-10-22
 */
@RestController
@RequestMapping("/u8c/trans-partner")
public class TransPartnerController {

    @Autowired
    private ITransPartnerService iTransPartnerService;

    @Autowired
    private IVendorService iVendorService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IMDMPartnerService imdmPartnerService;

    @Autowired
    private IMDMAdminOrgService imdmAdminOrgService;

    @Autowired
    private IMDMPartnerTypeService imdmPartnerTypeService;

    @Autowired
    private IMDMCodeItemsService imdmCodeItemsService;

    @Autowired
    private IBfpartnerzjbService bfpartnerzjbService;

    @Autowired
    private IMDMPartnerAreaService imdmPartnerAreaService;

    public final MDMAdminOrg getMdmAdmin() {
        return imdmAdminOrgService.getOne(new LambdaQueryWrapper<MDMAdminOrg>().eq(MDMAdminOrg::getNameChs, "贵州永红航空机械有限责任公司"));
    }

    public final MDMCodeItems getMDMCodeItems() {
        return imdmCodeItemsService.getOne(new LambdaQueryWrapper<MDMCodeItems>().like(MDMCodeItems::getNameChs, "个体工商户"));
    }

    public final MDMPartnerType getMDMPartnerType() {
        return imdmPartnerTypeService.getOne(new LambdaQueryWrapper<MDMPartnerType>().eq(MDMPartnerType::getNameChs, "集团外"));
    }

    @PostMapping("/getList")
    public JsonResult getList(VouchPagination pagination) {
        LambdaQueryWrapper<TransPartner> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(TransPartner::getId);
        Page<TransPartner> pageWhere = new Page<>(pagination.getPage().longValue(), pagination.getPageSize().longValue());
        IPage<TransPartner> pageData = iTransPartnerService.page(pageWhere, queryWrapper);
        return new JsonResult(true, pageData);
    }


    @PostMapping("/getThreeTable")
    public JsonResult getThreeTable(ThreePagination pagination) {
        ThreeResults threeResults = new ThreeResults();

        List<TransPartner> list = iTransPartnerService.list();
        List<TransPartner> vendorList=list;
        List<String> ids = list.stream().map(TransPartner::getLId).collect(Collectors.toList());

        List<String> cVenCodeList = iTransPartnerService.list(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getBelTable,"1")).stream().map(TransPartner::getCCusCode).collect(Collectors.toList());

        list.removeIf(partner->partner.getBelTable().equals("1"));
        List<String> cCusCodeList = list.stream().map(TransPartner::getCCusCode).collect(Collectors.toList());

        System.out.println("aaaaaaaaaaaaaaaaaaa");
        System.out.println(ids);
        System.out.println(cCusCodeList);
        System.out.println(cVenCodeList);

        LambdaQueryWrapper<Customer> customerWrapper = new LambdaQueryWrapper();
        customerWrapper.orderByDesc(Customer::getCCusCode);
        if(cCusCodeList.size()!=0){
            customerWrapper.and(wrapper->wrapper.notIn(Customer::getCCusCode,cCusCodeList));
        }
        if(StringUtils.isNotEmpty(pagination.getCCusName())){
            customerWrapper.like(Customer::getCCusName,pagination.getCCusName());
        }
        Page<Customer> cusWhere = new Page<>(pagination.getCustomerPage().longValue(), pagination.getCustomerPageSize().longValue());
        threeResults.setCustomerData(customerService.page(cusWhere, customerWrapper));

        LambdaQueryWrapper<Vendor> venWrapper = new LambdaQueryWrapper();
        venWrapper.orderByDesc(Vendor::getCVenCode);
        if(cVenCodeList.size()!=0){
            venWrapper.and(wrapper->wrapper.notIn(Vendor::getCVenCode,cVenCodeList));
        }
        if(StringUtils.isNotEmpty(pagination.getCVenName())){
            venWrapper.like(Vendor::getCVenName,pagination.getCVenName());
        }
        Page<Vendor> venWhere = new Page<>(pagination.getVendorPage().longValue(), pagination.getVendorPageSize().longValue());
        threeResults.setVendorData(iVendorService.page(venWhere, venWrapper));

        LambdaQueryWrapper<MDMPartner> partnerWrapper = new LambdaQueryWrapper();
        partnerWrapper.orderByDesc(MDMPartner::getId);
        if (ids.size()!=0){
            partnerWrapper.notIn(MDMPartner::getId,ids);
        }
        if(StringUtils.isNotEmpty(pagination.getNameChs())){
            partnerWrapper.like(MDMPartner::getNameChs,pagination.getNameChs());
        }
        Page<MDMPartner> pageWhere = new Page<>(pagination.getPartnerPage().longValue(), pagination.getPartnerPageSize().longValue());
        threeResults.setPartnerData(imdmPartnerService.page(pageWhere, partnerWrapper));

        return new JsonResult(true, threeResults);
    }

    @PostMapping("/autoConvert")
    public JsonResult autoConvert() {
        List<Customer> list = customerService.list(new LambdaQueryWrapper<Customer>().isNull(Customer::getDEndDate));
        for (Customer customer : list) {
            if(customer.getCCusRegCode() != null&& iTransPartnerService.list(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode,customer.getCCusCode()).eq(TransPartner::getCCusName,customer.getCCusName()))==null){
                List<MDMPartner> mdmPartners = imdmPartnerService.list(new LambdaQueryWrapper<MDMPartner>().eq(MDMPartner::getScc, customer.getCCusRegCode()).eq(MDMPartner::getNameChs, customer.getCCusName()));
                if(mdmPartners.size()!=0){
                    MDMPartner mdmPartner =mdmPartners.get(0);
                    TransPartner transPartner = new TransPartner();
                    transPartner.setLId(mdmPartner.getId());
                    transPartner.setCode(mdmPartner.getCode());
                    transPartner.setNameChs(mdmPartner.getNameChs());
                    transPartner.setPartnerarea(mdmPartner.getPartnerarea());
                    transPartner.setScc(mdmPartner.getScc());
                    transPartner.setBelTable("0");
                    transPartner.setCCusCode(customer.getCCusCode());
                    transPartner.setCCusName(customer.getCCusName());
                    transPartner.setCDCCode(customer.getCDCCode());
                    transPartner.setCCusRegCode(customer.getCCusRegCode());
                    iTransPartnerService.saveOrUpdate(transPartner);
                }else {
                    reportCustomer(customer);
                }
            }else {
                reportCustomer(customer);
            }
        }
        List<Vendor> vendors = iVendorService.list(new LambdaQueryWrapper<Vendor>().isNull(Vendor::getDEndDate));
        for (Vendor vendor : vendors) {
            if(vendor.getCVenRegCode() != null && iTransPartnerService.list(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode,vendor.getCVenCode()).eq(TransPartner::getCCusName,vendor.getCVenName()))==null){
                List<MDMPartner> mdmPartners = imdmPartnerService.list(new LambdaQueryWrapper<MDMPartner>().eq(MDMPartner::getScc, vendor.getCVenRegCode()).eq(MDMPartner::getNameChs, vendor.getCVenName()));
                if(mdmPartners.size()!=0){
                    MDMPartner mdmPartner =mdmPartners.get(0);
                    TransPartner transPartner = new TransPartner();
                    transPartner.setLId(mdmPartner.getId());
                    transPartner.setCode(mdmPartner.getCode());
                    transPartner.setNameChs(mdmPartner.getNameChs());
                    transPartner.setPartnerarea(mdmPartner.getPartnerarea());
                    transPartner.setScc(mdmPartner.getScc());
                    transPartner.setBelTable("1");
                    transPartner.setCCusCode(vendor.getCVenCode());
                    transPartner.setCCusName(vendor.getCVenName());
                    transPartner.setCDCCode(vendor.getCDCCode());
                    transPartner.setCCusRegCode(vendor.getCVenRegCode());
                    iTransPartnerService.saveOrUpdate(transPartner);
                }else {
                    reportVendor(vendor);
                }
            }else {
                reportVendor(vendor);
            }
        }
        return new JsonResult(true, "转换成功");
    }

    private void reportVendor(Vendor vendor) {
        Bfpartnerzjb bfpartnerzjb = new Bfpartnerzjb();
        bfpartnerzjb.setDomaintype("1");
        bfpartnerzjb.setInteriorcompany("0");
        bfpartnerzjb.setIsother("0");
        bfpartnerzjb.setSeclevel("0");
        bfpartnerzjb.setSeclevelid("BFPUBLIC");
        bfpartnerzjb.setOwnerorg(getMdmAdmin().getId());
        bfpartnerzjb.setUnitnature(getMDMCodeItems().getId());
        bfpartnerzjb.setType(getMDMPartnerType().getId());
        if (bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, vendor.getCVenName())) == null) {
            bfpartnerzjb.setId(UUID.randomUUID().toString());
            if (vendor.getCDCCode() != null) {
                List<MDMPartnerArea> list1 = imdmPartnerAreaService.list(new LambdaQueryWrapper<MDMPartnerArea>().eq(MDMPartnerArea::getCode, vendor.getCDCCode()));
                if(list1.size()!=0){
                    bfpartnerzjb.setArea(list1.get(0).getId());
                }
            }
            bfpartnerzjb.setCode(vendor.getCVenCode());
//                特别注意！！！社会信用代码暂时设置为必填，需要讨论出具体的解决方案出来
            if (vendor.getCVenRegCode() != null) {
                bfpartnerzjb.setOrganizationcode(vendor.getCVenRegCode());
                bfpartnerzjb.setCompanyorperson("0");
            } else {
                bfpartnerzjb.setCompanyorperson("1");
            }
            bfpartnerzjb.setIsvender("0");
            bfpartnerzjb.setIscustomer("1");
            if (vendor.getDEndDate() != null) {
                bfpartnerzjb.setStateIsenabled("0");
            } else {
                bfpartnerzjb.setStateIsenabled("1");
            }
            bfpartnerzjb.setNameChs(vendor.getCVenName());
            bfpartnerzjbService.save(bfpartnerzjb);
        }

    }

    public void reportCustomer(Customer customer){
        Bfpartnerzjb bfpartnerzjb = new Bfpartnerzjb();
        bfpartnerzjb.setDomaintype("1");
        bfpartnerzjb.setInteriorcompany("0");
        bfpartnerzjb.setIsother("0");
        bfpartnerzjb.setSeclevel("0");
        bfpartnerzjb.setSeclevelid("BFPUBLIC");
        bfpartnerzjb.setOwnerorg(getMdmAdmin().getId());
        bfpartnerzjb.setUnitnature(getMDMCodeItems().getId());
        bfpartnerzjb.setType(getMDMPartnerType().getId());
        if (bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, customer.getCCusName())) == null) {
            bfpartnerzjb.setId(UUID.randomUUID().toString());
            if (customer.getCDCCode() != null) {
                List<MDMPartnerArea> list1 = imdmPartnerAreaService.list(new LambdaQueryWrapper<MDMPartnerArea>().eq(MDMPartnerArea::getCode, customer.getCDCCode()));
                if(list1.size()!=0){
                    bfpartnerzjb.setArea(list1.get(0).getId());
                }
            }
            bfpartnerzjb.setCode(customer.getCCusCode());
//                特别注意！！！社会信用代码暂时设置为必填，需要讨论出具体的解决方案出来
            if (customer.getCCusRegCode() != null) {
                bfpartnerzjb.setOrganizationcode(customer.getCCusRegCode());
                bfpartnerzjb.setCompanyorperson("0");
            } else {
                bfpartnerzjb.setCompanyorperson("1");
            }
            bfpartnerzjb.setIsvender("0");
            bfpartnerzjb.setIscustomer("1");
            if (customer.getDEndDate() != null) {
                bfpartnerzjb.setStateIsenabled("0");
            } else {
                bfpartnerzjb.setStateIsenabled("1");
            }
            bfpartnerzjb.setNameChs(customer.getCCusName());
            bfpartnerzjbService.save(bfpartnerzjb);
        }
    }
}
